package com.cy.pj.sys.service.impl;

import com.cy.pj.sys.common.ServiceException;
import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.pojo.Node;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysLogService;
import com.cy.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.chrono.IsoChronology;
import java.util.List;
import java.util.Map;
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    @Override
    public List<Map<String, Object>> findObjects() {
        List<Map<String, Object>> list = sysMenuDao.findObjects();
        if (list == null || list.size() == 0) {
            throw new ServiceException("没有对应的菜单信息");
        }
        return list;
    }

    @Override
    public int deleteObject(Integer id) {
        if (id == null || id <= 0) {
            throw new ServiceException("请选择");
        }
        int count = sysMenuDao.getChildCount(id);
        if (count > 0) {
            throw new ServiceException("请先删除子菜单");
        }
        sysRoleMenuDao.deleteObjectByMenuId(id);
        int row = sysMenuDao.deleteObject(id);
        if (row == 0) {
            throw new ServiceException("菜单记录可能已经不错在");
        }
        return row;
    }

    @Override
    public int insertObject(SysMenu sysMenu) {
        if (sysMenu == null) {
            throw new ServiceException("保存对象不能为空");
        }
        if (StringUtils.isEmpty(sysMenu.getName())) {
            throw new ServiceException("菜单名不能为空");
        }
        int rows = sysMenuDao.insertObject(sysMenu);
        return rows;
    }

    @Override
    public List<Node> findZtreeMenuNodes() {
        return sysMenuDao.findZtreeMenuNodes();
    }

    @Override
    public int updateObject(SysMenu sysMenu) {
        if (sysMenu == null) {
            throw new ServiceException("更新对象不能为空");
        }
        if (StringUtils.isEmpty(sysMenu.getName())) {
            throw new ServiceException("菜单名不能为空");
        }
        int rows = sysMenuDao.insertObject(sysMenu);
        if (rows == 0) {
            throw new ServiceException("菜单记录可能已不存在");
        }
        return rows;
    }
}
